package com.tt.fashionWebSite.dao.jpa;

import com.tt.fashionWebSite.dao.ChiTietKhuyenMaiDao;
import com.tt.fashionWebSite.domain.ChiTietHoaDon;
import com.tt.fashionWebSite.domain.ChiTietKhuyenMai;
import com.tt.fashionWebSite.domain.ChiTietSanPham;
import com.tt.fashionWebSite.dto.ChiTietKhuyenMaiFullDto;
import org.springframework.stereotype.Repository;

import javax.persistence.Query;
import java.util.ArrayList;
import java.util.List;

@Repository("chiTietKhuyenMai")
public class ChiTietKhuyenMaiJpaDao extends GenericJpaDao<ChiTietKhuyenMai, String> implements ChiTietKhuyenMaiDao {
    public ChiTietKhuyenMaiJpaDao() {
        super(ChiTietKhuyenMai.class);
    }

    @Override
    public List<ChiTietKhuyenMai> getAllDealDetail(){
        return getEntityManager().createNamedQuery(ChiTietKhuyenMai.GET_DEALDETAIL_ALL, ChiTietKhuyenMai.class).getResultList();
    }

    @Override
    public List<ChiTietKhuyenMaiFullDto> getAllDealDetailByProduct(String maCTSP){
        String strQuery = "SELECT "
              + "k.NGAY_BD, "
              + "k.NGAY_KT, "
              + "s.PT_GIA_KHUYEN_MAI "
              + "FROM chi_tiet_km s "
              + "JOIN khuyen_mai k "
                + "ON s.MA_KM = k.MA_KM "
              + "WHERE s.MA_CT_SP = :maCTSP ";

        Query query = getEntityManager().createNativeQuery(strQuery);
        query.setParameter("maCTSP", maCTSP);

        List<Object> results = query.getResultList();
        List<ChiTietKhuyenMaiFullDto> chiTietKhuyenMaiFullDtos = new ArrayList<ChiTietKhuyenMaiFullDto>();
        ChiTietKhuyenMaiFullDto chiTietKhuyenMaiFullDto = null;
        for (int i = 0; i < results.size(); i++) {
            Object[] userDB = (Object[]) results.get(i);
            chiTietKhuyenMaiFullDto = new ChiTietKhuyenMaiFullDto(userDB);
            chiTietKhuyenMaiFullDtos.add(chiTietKhuyenMaiFullDto);
        }
        return chiTietKhuyenMaiFullDtos;
    }

    @Override
    public List<ChiTietKhuyenMai> getDataDetailOfDealCommon(String dealId) {
        return getEntityManager().createNamedQuery(ChiTietKhuyenMai.GET_SEARCH_DEALDETAIL_OF_ID, ChiTietKhuyenMai.class)
                .setParameter("dealId", dealId)
                .getResultList();
    }
}
